<?php
    $lien = "les-menus";
    $template = "menus";
    $title = "Les menus";
    
    $cat = modele_category::findByPermalien($lien);
    if($cat===null){
        $cat = new modele_category();
        $datas = array(
            'title'=>$title,
            'lien'=>$lien,
            'template'=>PWE::getDefaultTemplate()
        );
        $cat->setDatas($datas);
        $cat->save();
    }
?>

<?php if(navigation::getAdminAction() === 'create' || navigation::getAdminAction() === 'update' ): ?>
<?php
if(navigation::getAdminModeleId()>0){
    $publication = new modele_publication(navigation::getAdminModeleId());
    $data = $publication->getDatas();
    $data['permalien'] = $publication->getPermalien()->getCleanedLien(false);
    $action = 'update';
}else{
    $action = 'create';
    $data = array();
}
?>
<h1><a class="pwe-ui-btn" title="Retour" href="?template=<?php echo $template; ?>"><span class="ui-icon ui-icon ui-icon-arrowthick-1-w"></span></a> Ajouter/modifier un menu</h1>
<hr style="margin: 5px 0 15px 0;">
<form method="post" enctype="multipart/form-data" action="?template=<?php echo $template; ?>&action=<?php echo $action; ?>&modele=publication&do=1&modele_id=<?php echo isset($data['id'])?$data['id']:0; ?>">
    <div class="columns ten">
        <p style="margin: 10px 0;"><input type="text" name="modele_title" value="<?php echo (isset($data['title']))?$data['title']:''; ?>" class="titreModele"  onblur="javascript:generatePermalien($(this).val());"/></p>
        <p style="margin: 10px 0;"><?php echo PWE::getBaseURL(); ?><input id="permalien" type="text" name="modele_lien" value="<?php echo (isset($data['permalien']))?$data['permalien']:''; ?>" style="font-size: 1.2em;" onblur="javascript:validPermalien($(this).val());"/><?php echo modele_permalien::getExtensionByModele('publication'); ?></p>
        <h3>Contenu de menu</h3>
        <textarea class="editor" name="modele_content"><?php echo (isset($data['content']))?$data['content']:''; ?></textarea><br/>
        <p style="margin: 10px 0;">Titre SEO : <input type="text" maxlength="255" name="modele_titre_seo" value="<?php echo (isset($data['titre_seo']))?$data['titre_seo']:''; ?>" style="width: 450px;"/></p>
        <p style="margin: 10px 0;">Description SEO : <textarea  maxlength="512" name="modele_description_seo" style="height: 100px;"><?php echo (isset($data['description_seo']))?$data['description_seo']:''; ?></textarea></p>
        <div align="right">
            <input type="submit" value="Enregistrer" class="pwe-ui-btn"/> 
            <?php if(isset($data['id'])&&$data['id']>0): ?><a href="<?php echo $data['url']; ?>" target="_blank" title="voir la publication" class="pwe-ui-btn">voir</a><?php endif; ?>
        </div>
        
        <?php include('../admin/include/inc/redirectionManager.inc.php'); ?>
        
    </div>
    <div class="columns five">
        <h3>Statut</h3>
        <select name="modele_statut_id">
            <option value="1" <?php echo(isset($data['statut_id'])&&$data['statut_id']=='1')?'selected="selected"':''; ?>>Brouillon</option>
            <option value="2" <?php echo(isset($data['statut_id'])&&$data['statut_id']=='2')?'selected="selected"':''; ?>>Publié</option>
        </select><br/><br/>
        <?php include('../admin/include/inc/templateManager.inc.php'); ?>        
        <?php include('../admin/include/inc/mediaManager.inc.php'); ?>
        <input type="hidden" name="modele_id" value="<?php echo (isset($data['id']))?$data['id']:0; ?>" />
        <input type="hidden" name="modele_category_id" value="<?php echo $cat->getId(); ?>" />
    </div>
</form>
<?php else: ?>
<?php
if(isset($_POST) && isset($_POST['save_order'])){
    foreach($_POST as $var=>$value){
        if(substr($var,0,6)==='order_'){
            list($s, $idp) = explode('_',$var,2);
            $publication = new modele_publication($idp);
            $publication->setOrder($value);
        }
    }
}
?>
<h1>Page "nos menus"</h1>
<hr style="margin: 5px 0 25px 0;">
<form method="post">
    <div class="columns ten">
        <div align="right"><a href="<?php echo $cat->getUrl(); ?>" class="pwe-ui-btn" target="preview_site">Voir</a> <a class="pwe-ui-btn" href="?template=<?php echo $template; ?>&modele=publication&action=create">Nouveau</a><br/><br/></div>
        <table class="liste ui-widget table_sortable">
            <thead class="ui-widget-header">
                <tr>
                    <th>Ordre</th>
                    <th>Titre</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody class="liste-items-menu">
                <?php foreach($cat->getPublications() as $i=>$menus): ?>
                <tr class="ui-widget-content" style="cursor: move;">
                    <td class="orderValueCell">
                        <span class="value"><?php echo $i+1; ?></span>
                        <input type="hidden" class="orderValue" name="order_<?php echo $menus->getId(); ?>" value="<?php echo $i+1; ?>" />
                    </td>
                    <td>
                        <span><?php echo $menus->getTitle(); ?></span><br/>
                    </td>
                    <td align="right">
                        <a class="pwe-ui-btn" title="Modifier" href="?template=<?php echo $template; ?>&modele=publication&action=update&modele_id=<?php echo $menus->getId(); ?>"><span class="ui-icon ui-icon-pencil"></span></a>
                        <a href="?template=<?php echo $template; ?>&modele=publication&action=delete&do=1&modele_id=<?php echo $menus->getId(); ?>" class="confirmAction pwe-ui-btn" title="Supprimer '<?php echo $menus->getTitle(); ?>'"><span class="ui-icon ui-icon-trash"></span></a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot></tfoot>
        </table><br/>
        <div align="right"><br/>(modifier l'ordre en déplassant les lignes par glisser/déposer) <input class="pwe-ui-btn" type="submit" name="save_order" value="enregistrer l'ordre" /></div>
    </div>
    <div class="columns five">
        <?php $data = $cat->getDatas(); ?>
        <?php include('../admin/include/inc/mediaManager.inc.php'); ?>
    </div>
</form>
<?php endif; ?>